/**
 * Created by hjqi on 2016/8/30.
 */
var express = require('express');
var router = express.Router();

var libCluster = require('../lib/cluster/cluster');
var _lib_cluster = new libCluster();

var common = require('../lib/common/common');
var _k8s_adp = require('../lib/k8s/k8sAdpter');


router.get('/get',function(req, res, next){
    var _data =  req.query;
    var _username = req.username;

    _lib_cluster.getClusters(_username,_data.clusterId).then(function(rets){
        res.send(rets);
    }).catch(function(err){
        res.status(400);
        res.send(err.message);
    });

});

router.post('/checkNode',function(req, res, next){
    var _data =  req.body;
    var _username = req.username;

    var _token = _data.token;
    /*var _token_data = common.decodeTokenData(_token);
    _k8s_adp.getK8s(_data.clusterId).then(function(_k8s){
        return _k8s.getNodesInfo();
    }).then(function(ret){
        if(ret.indexOf('not found') !== -1)
            res.send({ret:false});
        else
            res.send({
                ret:_lib_cluster.checkClusterNode_v2(ret,_token_data.id)
            });
    })*/
    _lib_cluster.checkClusterNode(_username,_data.clusterId,_token).then(function(rets){
        if(rets.length > 0){
            res.send({
                ret:true
            });
        }else{
            res.send({
                ret:false
            });
        }
    }).catch(function(err){
        res.status(400);
        res.send(err.message);
    });

});

router.post('/getNodeInfoByPage',function(req, res, next) {
    var _data = req.body;
    var _username = req.username;

    var _nodes = _data.nodes;



    _k8s_adp.getK8s(_data.clusterId)
    .then(function(_k8s){
        return _k8s.getNodesInfo();
     }).then(function(ret){
        if(ret.indexOf('not found') !== -1)
             res.send({ret:false,nodes:_nodes});
        else{
            res.send({ret:true,nodes:_lib_cluster.setClusterNodeInfo(ret,_nodes)});
        }

     })
     .catch(function(err){
        res.status(400);
        res.send(err.message);
    });

});

router.post('/getNodeInfo',function(req, res, next){
    var _data =  req.body;
    var _username = req.username;

    _k8s_adp.getK8s(_data.clusterId).then(function(_k8s){
        return _k8s.getNodesInfo();
    }).then(function(ret){
            if(ret.indexOf('not found') !== -1)
                res.send({ret:false,nodes:[]});
            else{
                res.send({ret:true,nodes:JSON.parse(ret).items});
            }
        })
        .catch(function(err){
            res.status(400);
            res.send(err.message);
        });

});

router.get('/getClusterToken',function(req, res, next){
    var _data =  req.query;
    var _username = req.username;

    _lib_cluster.getClusterToken(_username,_data.clusterId).then(function(token){
        res.send({
            ret:true,
            data:token
        });
    }).catch(function(err){
        res.status(400);
        res.send(err.message);
    });

});

router.post('/delete',function(req, res, next){
    var _data =  req.body;
    var _username = req.username;

    _lib_cluster.deleteCluster(_username,_data.clusterId).then(function(ret){
        res.send(ret);
    }).catch(function(err){
        res.status(400);
        res.send(err);
    });

});

router.post('/create',function(req, res, next){
    var _data =  req.body;
    var _username = req.username;

    _lib_cluster.createCluster(_username,_data.name).then(function(ret){
        res.send(ret);
    }).catch(function(err){
        res.status(400);
        res.send(err);
    });
});

router.post('/deleteNode',function(req, res, next){

    var _data =  req.body;
    var _username = req.username;

    _lib_cluster.deleteClusterNode(_username,_data.clusterId,_data.nodeId).then(function(ret){
        res.send(ret);
    }).catch(function(err){
        res.status(400);
        res.send(err);
    });

});

router.post('/install',function(req, res, next){

    var _data =  req.body;
    var _username = req.username;

    _lib_cluster.deleteClusterNode(_username,_data.clusterId,_data.nodeId).then(function(ret){
        res.send(ret);
    }).catch(function(err){
        res.status(400);
        res.send(err);
    });

});

module.exports = router;